package com.xw.echo.core.client;

import com.alibaba.nacos.api.naming.NamingService;
import com.xw.echo.core.cluster.Cluster;
import com.xw.echo.core.cluster.ClusterClient;
import com.xw.echo.core.cluster.LoadBalancer;
import com.xw.echo.core.cluster.RoundRobinLoadBalance;
import com.xw.echo.core.nacos.client.NacosConsumer;
import com.xw.echo.core.proxy.JDKProxy;
import lombok.Data;

@Data
public class EchoClusterClient extends EchoClientAbstract {
    private Cluster cluster;
    private String serviceName;
    private NacosConsumer nacosConsumer;
   private LoadBalancer loadBalancer;
    public EchoClusterClient(String serviceName, NamingService namingService) {
        this.serviceName = serviceName;
        this.nacosConsumer = new NacosConsumer(namingService,serviceName);
        //初始化集群线程池
        managerConnectPool.initClusterConnectionPool(serviceName, nacosConsumer.getAllInstanceIpAndPort());
    }
    @Override
    public Object creatProxy(Class<?> clazz) {
        this.cluster = new ClusterClient(serviceName);
        JDKProxy jdkProxy=null;
        if (loadBalancer != null){
             jdkProxy = new JDKProxy(clazz, cluster,nacosConsumer,loadBalancer);
        }else {
            jdkProxy = new JDKProxy(clazz, cluster,nacosConsumer,new RoundRobinLoadBalance());
        }
        return jdkProxy.createProxy();
    }


}
